package com.jinyuan.options;

import com.jinyuan.R;
import com.jinyuan.options.about.AboutFragment;
import com.jinyuan.options.homepage.HomePageFragment;
import com.jinyuan.options.setting.SettingFragment;
import com.jinyuan.view.dialog.LoadingDialog;
import com.jinyuan.view.slidingmenuview.SlidingMenuUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

public class OptionsFragment extends Fragment {

	protected static LayoutInflater inflater;
	protected int position, viewId;
	private String key = "bundle";
	protected static View main_header_LinearLayout;
	public static LoadingDialog loadingDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			if (getArguments().containsKey(MainActivity.Position)) {
				position = getArguments().getInt(MainActivity.Position);
			}
			if (getArguments().containsKey(MainActivity.ViewId)) {
				viewId = getArguments().getInt(MainActivity.ViewId);
			}
		}
		loadingDialog = new LoadingDialog(this.getActivity(), clickListener);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		OptionsFragment.inflater = inflater;
		View options = inflater.inflate(R.layout.options_framelayout,
				container, false);
		OptionsFragment fragment = null;
		switch (viewId) {
		case R.id.composer_buttons_wrapper:
			//-- 中心菜单
		case R.id.leftmenu_commonmenu_ListView:
			//-- 左侧常用菜单
			switch (position) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				break;
			case 8:
				break;
			default:
				break;
			}
			break;
		case R.id.leftmenu_operationmenu_ListView:
			//-- 左侧操作菜单
			switch (position) {
			case 0:
				fragment = new SettingFragment();
				break;
			case 1:
				fragment = new AboutFragment();
				break;
			default:
				break;
			}
			break;
		case R.id.main_tab_RadioGroup:
			//-- 底部菜单
			switch (position) {
			case R.id.main_tab_1_RadioButton:
				fragment = new HomePageFragment();
				break;
			case R.id.main_tab_2_RadioButton:
				fragment = new SettingFragment();
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		if (fragment != null) {
			toFragment(fragment, fragment.getClass().getSimpleName());
		}
		main_header_LinearLayout = ((View) container.getParent())
				.findViewById(R.id.main_header_LinearLayout);
		return options;
	}

	/**
	 * 跳转页面
	 * 
	 * @param fragment
	 *            目标Fragment
	 * @param fragmentName
	 *            目标Fragment类名
	 */
	protected void toFragment(OptionsFragment fragment, String fragmentName) {
		toFragment(R.id.options_FrameLayout, fragment, fragmentName);
	}
	
	protected void toFragment(OptionsFragment fragment, String fragmentName, Object... objects) {
		toFragment(R.id.options_FrameLayout, fragment, fragmentName, objects);
	}

	protected void toFragmentNoBack(OptionsFragment fragment) {
		toFragmentNoBack(R.id.options_FrameLayout, fragment);
	}

	/**
	 * 跳转页面
	 * 
	 * @param layout
	 *            被替换的layout
	 * @param fragment
	 *            目标Fragment
	 * @param fragmentName
	 *            目标Fragment类名
	 */
	protected void toFragment(int layout, OptionsFragment fragment,
			String fragmentName) {
		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(layout, fragment).addToBackStack(fragmentName)
				.commit();
	}

	protected void toFragmentNoBack(int layout, OptionsFragment fragment) {
		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(layout, fragment).commit();
	}

	public static void popBackStack(FragmentActivity fragmentActivity) {
		int count = fragmentActivity.getSupportFragmentManager()
				.getBackStackEntryCount();
		for (int j = 0; j < count; j++) {
			int backStackId = fragmentActivity.getSupportFragmentManager()
					.getBackStackEntryAt(j).getId();
//			String backStackName = fragmentActivity.getSupportFragmentManager()
//					.getBackStackEntryAt(j).getName();
//			System.out.println("-----backStackId-----" + backStackId);
//			System.out.println("-----backStackName-----" + backStackName);
			fragmentActivity.getSupportFragmentManager().popBackStack(
					backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		}
	}

	/**
	 * 跳转页面
	 * 
	 * @param layout
	 *            被替换的layout
	 * @param fragment
	 *            目标Fragment
	 * @param fragmentName
	 *            目标Fragment类名
	 * @param objects
	 *            要传递的数据
	 */
	protected void toFragment(int layout, OptionsFragment fragment,
			String fragmentName, Object... objects) {
		if (fragment != null) {
			Bundle arguments = new Bundle();
			arguments.putSerializable(key, objects);
			fragment.setArguments(arguments);
			getActivity().getSupportFragmentManager().beginTransaction()
					.replace(layout, fragment).addToBackStack(fragmentName)
					.commit();
		}
	}

	protected void toFragmentNoBack(int layout, OptionsFragment fragment,
			Object... objects) {
		if (fragment != null) {
			Bundle arguments = new Bundle();
			arguments.putSerializable(key, objects);
			fragment.setArguments(arguments);
			getActivity().getSupportFragmentManager().beginTransaction()
					.replace(layout, fragment).commit();
		}
	}

	protected ListView getGeneralListView() {
		ListView listView = (ListView) inflater.inflate(R.layout.general_listview, null);
		View footer = inflater.inflate(R.layout.general_listview_footer, null);
		listView.addFooterView(footer);
		return listView;
	}
	
	/**
	 * 设置传递数据
	 * 
	 * @param objects
	 */
	protected void setBundle(Object... objects) {
		Bundle arguments = new Bundle();
		arguments.putSerializable(key, objects);
		OptionsFragment fragment = new OptionsFragment();
		fragment.setArguments(arguments);
	}

	/**
	 * 获取传递数据
	 * 
	 * @return
	 */
	protected Object[] getBundle() {
		if (getArguments() != null) {
			if (getArguments().containsKey(key)) {
				Object[] object = (Object[]) getArguments()
						.getSerializable(key);
				return object;
			}
		}
		return null;
	}

	protected void setTitle(int title) {
		if (main_header_LinearLayout != null) {
			TextView titleTextView = (TextView) main_header_LinearLayout
					.findViewById(R.id.main_header_title_TextView);
			if (titleTextView != null) {
				titleTextView.setText(title);
			}
		}
	}

	/**
	 * header右端按钮
	 * 
	 * @param view
	 */
	protected void setRightFrameLayout(View view) {
		if (main_header_LinearLayout != null) {
			FrameLayout right = (FrameLayout) main_header_LinearLayout
					.findViewById(R.id.main_header_title_TextView);
			if (right != null) {
				right.removeAllViews();
				right.addView(view);
			}
		}
	}

	/**
	 * header右端按钮
	 * 
	 * @param view
	 * @param clickListener
	 */
	protected void setRightFrameLayout(int viewId, OnClickListener clickListener) {
		if (main_header_LinearLayout != null) {
			FrameLayout right = (FrameLayout) main_header_LinearLayout
					.findViewById(R.id.main_header_title_TextView);
			View view = inflater.inflate(viewId, null);
			view.setOnClickListener(clickListener);
			if (right != null) {
				right.removeAllViews();
				right.addView(view);
			}
		}
	}

	/**
	 * header右端按钮是否显示
	 * 
	 * @param visibility
	 */
	protected void setRightFrameLayoutVisibility(int visibility) {
		if (main_header_LinearLayout != null) {
			FrameLayout right = (FrameLayout) main_header_LinearLayout
					.findViewById(R.id.main_header_title_TextView);
			right.setVisibility(visibility);
		}
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.loadingdialog_cancel_ImageView:
				loadingDialog.cancel();
				break;

			default:
				break;
			}

		}
	};

	@Override
	public void onStart() {
		super.onStart();
		SlidingMenuUtil.leftIsGone = false;
	}

}
